package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.Painter;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;

public class CompInicioFin extends Componente {

	final static int Radius = 20;
	final static int InternalRadius = 15;
	public final static int MODO_INICIO = 0;
	public final static int MODO_FIN = 1;
	private int mode;

	public CompInicioFin() {
		_centerPoint = new Point(0, 0);
		_width = Radius * 2;
		_height = Radius * 2;
	}

	@Override
	public void setTitulo(String titulo) {

	}

	public void setMode(int Mode) {
		mode = Mode;
	}

	public int getMode() {
		return mode;
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		int posX = _centerPoint.x + offSetX;
		int posY = _centerPoint.y + offSetY;

		if (mode == MODO_INICIO) {
			c.drawCircle(posX * globalScale, posY * globalScale, Radius
					* globalScale,
					Painter.getInstancia().getBarPaint(_currentState));
		} else {
			c.drawCircle(posX * globalScale, posY * globalScale, Radius
					* globalScale,
					Painter.getInstancia().getBorderPaint(_currentState));
			c.drawCircle(posX * globalScale, posY * globalScale, InternalRadius
					* globalScale,
					Painter.getInstancia().getBarPaint(_currentState));
		}

		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}
	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		int CompX = _centerPoint.x - touch.x;
		int CompY = _centerPoint.y - touch.y;
		if ((CompX * CompX + CompY * CompY) <= (Radius * Radius)) {
			return true;
		}
		return false;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsBegining) {
				value.CalcularConexiones(this, VistaDiagrama.getVista()
						.getDiagrama().getComponenteById(value._childId));
			}
		}
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsEnd) {
				value.CalcularConexiones(VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(value._fatherId), this);
			}
		}
	}

	@Override
	public void actualizarPuntoCentral() {
		_centerPoint.x = (int) (Anchor.x + (_width / 2));
		_centerPoint.y = (int) (Anchor.y + (_height / 2));
	}

	@Override
	public String generarDOM() {
		String DOM = "\t<Componente Tipo=\"InicioFin\" Titulo=\"" + Titulo
				+ "\" ID=\"" + ID + "\" AnchorX=\"" + Anchor.x
				+ "\" AnchorY=\"" + Anchor.y + "\" Modo=\"" + mode +"\">\n";
		DOM = DOM + "\t</Componente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		// TODO Auto-generated method stub

	}

	@Override
	public Point requestConnectionPoint(CompConector connector, int mode,
			Point otherCompCenter) {
		float dX = (otherCompCenter.x - _centerPoint.x);
		float dY = (otherCompCenter.y - _centerPoint.y);
		if (dY == 0) {
			return otherCompCenter;
		} else {
			double m = dY / dX;
			if (_centerPoint.x > otherCompCenter.x) {
				return calcularPuntoExterno(m, true);
			} else {
				return calcularPuntoExterno(m, false);
			}
		}
	}

	private Point calcularPuntoExterno(double m, boolean inverse) {
		Point Respuesta = new Point();
		double angle = Math.atan(m);
		if (inverse) {
			Respuesta.x = _centerPoint.x - (int) (Radius * Math.cos(angle));
			Respuesta.y = _centerPoint.y - (int) (Radius * Math.sin(angle));
		} else {
			Respuesta.x = _centerPoint.x + (int) (Radius * Math.cos(angle));
			Respuesta.y = _centerPoint.y + (int) (Radius * Math.sin(angle));
		}

		return Respuesta;
	}

	@Override
	public void mover(int distX, int distY) {
		Anchor.x += distX;
		Anchor.y += distY;
		actualizarPuntoCentral();
		actualizarAchorsPoints();

	}

	@Override
	public String getTipo() {
		if (mode == MODO_INICIO) {
			return "InicioAct";
		} else {
			return "FinAct";
		}
	}

	@Override
	public void setAnchor(Point anchor) {
		Anchor = anchor;
		actualizarPuntoCentral();
	}
}
